//
//  PBHomeViewController.m
//  PBHomeDemo
//
//  Created by Playboy on 2017/11/21.
//  Copyright © 2017年 tmkj. All rights reserved.
//

#import "PBHomeViewController.h"
#import "PBHomeTypeCell.h"
#import "PBAdsCell.h"
#import "PBHomeCell.h"
#import "PBHomeDetailController.h"
#import "PBSearchHomeController.h"
#import "PBChangeCityController.h"
#import "PBHomeModel.h"
#import "PBBestHomeModel.h"
#import "PBHomeTypeView.h"
#import "PBHomeBestCell.h"
#import "PBHomeLifeCell.h"
#import "PBHomeDownCell.h"
#import "PBMyMessageController.h"
#import "PBLocation.h"

static NSString * const kPBHomeTypeCell = @"PBHomeTypeCell";
static NSString * const kPBAdsCell = @"PBAdsCell";
static NSString * const kPBHomeCell = @"PBHomeCell";
static NSString * const kPBHomeBestCell = @"PBHomeBestCell";
static NSString * const kPBHomeLifeCell = @"PBHomeLifeCell";
static NSString * const kPBHomeDownCell = @"PBHomeDownCell";

@interface PBHomeViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (strong, nonatomic) IBOutlet UIButton *cityButton;
@property (strong, nonatomic) IBOutlet UIView *searchView;
@property (strong, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *adArray;

@end

@implementation PBHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self configure];
}
- (void)configure {
    [self configureView];
    [self configureData];
//    [TMNetwork requestGetWithParam:@{} url:@"/tag/getAll" callback:^(TMBaseReqModel *model) {
//
//    }];
//    [TMNetwork requestGetWithParam:@{@"type":@"1"} url:@"/tag/getTagListByType" callback:^(TMBaseReqModel *model) {
//
//    }];
//    [TMNetwork requestGetWithParam:@{@"type":@"2"} url:@"/tag/getTagListByType" callback:^(TMBaseReqModel *model) {
//
//    }];
    
    // 获取当前位置
    [[PBLocation location] startLocationAddress:^(BOOL isSuccess, PBLocationModel *locationModel) {
        if (isSuccess) {
            [TMShareData shareData].currentLocation = locationModel.currentLocation;
        }else {
            NSLog(@"定位失败");
        }
    }];
    
}

- (void)configureView {
    [TMShareData shareData].cityId = @"5001";
    self.searchView.layer.cornerRadius = 5;
    self.searchView.layer.masksToBounds = YES;
    [self.tableView registerNib:[UINib nibWithNibName:kPBHomeTypeCell bundle:nil] forCellReuseIdentifier:kPBHomeTypeCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBAdsCell bundle:nil] forCellReuseIdentifier:kPBAdsCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBHomeCell bundle:nil] forCellReuseIdentifier:kPBHomeCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBHomeBestCell bundle:nil] forCellReuseIdentifier:kPBHomeBestCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBHomeLifeCell bundle:nil] forCellReuseIdentifier:kPBHomeLifeCell];
    [self.tableView registerNib:[UINib nibWithNibName:kPBHomeDownCell bundle:nil] forCellReuseIdentifier:kPBHomeDownCell];
    UITapGestureRecognizer *searchTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterSearchVC)];
    [self.searchView addGestureRecognizer:searchTap];
    if (IS_IPHONE_X) {
        self.topConstraint.constant = 44;
    }else {
        self.topConstraint.constant = 22;
    }
    // 刷新
    [TMRefreshHelper headerRefresh:self.tableView withTarget:self action:@selector(headerRefresh)];
}
- (void)configureData {
    // 广告
    NSDictionary *param = @{@"cityId":[TMShareData shareData].cityId,
                            @"type":@"1"};
    [TMNetwork requestGetWithParam:param url:@"/banner/list" callback:^(TMBaseReqModel *model) {
        [self.tableView.mj_header endRefreshing];
        if (model.code == 200) {
            self.adArray = [PBHomeModel objectArrayWithKeyValuesArray:model.data];
            [self.tableView reloadData];
        }else {
            [self showHint:model.message];
        }
    }];
}

- (void)headerRefresh {
    [self configureData];
}

#pragma mark - <UITableViewDelegate,UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        PBAdsCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBAdsCell forIndexPath:indexPath];
        if (self.adArray.count>0) {
            cell.jumpVC = self;
            [cell refreshWithArray:self.adArray];
        }
        return cell;
    }else{
        if (indexPath.row < 4) {
            PBHomeBestCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBHomeBestCell forIndexPath:indexPath];
            cell.jumpVC = self;
            [cell refreshWithIndexPath:indexPath];
            return cell;
        }else if (indexPath.row == 4) {
            PBHomeLifeCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBHomeLifeCell forIndexPath:indexPath];
            cell.jumpVC = self;
            return cell;
        }else {
            PBHomeDownCell *cell = [tableView dequeueReusableCellWithIdentifier:kPBHomeDownCell forIndexPath:indexPath];
            cell.jumpVC = self;
            return cell;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 200;
    }else {
        if (indexPath.row == 4) {
            return 180;
        }else if (indexPath.row == 5) {
            return 300;
        }else {
            return 280;
        }
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 85;
    }
    return 0.01f;
}
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    if (indexPath.section > 0) {
//        PBBestHomeModel *model = self.dataSource[indexPath.row];
//        PBHomeDetailController *vc = [[PBHomeDetailController alloc] init];
//        vc.hidesBottomBarWhenPushed = YES;
//        vc.houseId = [NSString stringWithFormat:@"%zd",model.ID];
//        [self.navigationController pushViewController:vc animated:YES];
//    }
//}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        PBHomeTypeView *header = [[[NSBundle mainBundle] loadNibNamed:@"PBHomeTypeView" owner:nil options:nil] lastObject];
        header.jumpVC = self;
        return header;
    }
    return nil;
}


#pragma mark - click method
- (void)enterSearchVC {
    PBSearchHomeController *vc = [[PBSearchHomeController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)cityButtonClick:(UIButton *)sender {
    kWeakSelf(weakSelf)
    PBChangeCityController *vc = [[PBChangeCityController alloc] init];
    [vc setBackBlcok:^(NSString *cityName, NSString *cityId) {
        [weakSelf.cityButton setTitle:cityName forState:UIControlStateNormal];
        [TMShareData shareData].cityId = cityId;
        [weakSelf configureData];
    }];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)messageButtonClick:(UIButton *)sender {
    PBMyMessageController *vc = [[PBMyMessageController alloc] init];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - super
- (void)resetNavigationBar {
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
